package io.renren.modules.seller.controller;

import io.renren.modules.seller.entity.PageResult;
import io.renren.modules.seller.entity.Result;
import io.renren.modules.seller.entity.SpecificationEntity;
import io.renren.modules.seller.group.Specification;
import io.renren.modules.seller.service.SpecificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mark  15373178086@163.com
 * @date 2020-02-16 11:30:55
 */
@Slf4j
@RestController
@RequestMapping("manage/specification")
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;

    /**
     * 返回全部列表
     *
     * @return SpecificationEntitys
     */
    @RequestMapping("/findAll")
    public List<SpecificationEntity> findAll() {
        return specificationService.findAll();
    }

    /**
     * 返回全部列表
     *
     * @return PageResult
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestParam Map<String, Object> params) {
        log.info("spec findPage params:{}", params);
        return specificationService.findPage(params);
    }

    /**
     * 查询+分页
     *
     * @return PageResult
     */
    @RequestMapping("/search")
    public PageResult search(@RequestBody SpecificationEntity spec, String page, String limit) {
        Map<String, Object> params = new HashMap<>(8);
        params.put("page", page);
        params.put("limit", limit);
        params.put("specName", spec.getSpecName());
        log.info("brand search params:{}", params);
        return specificationService.findPage(params);
    }

    /**
     * 增加
     *
     * @return Result
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Specification specification) {
        log.info("spec add params:{}", specification);
        try {
            specificationService.add(specification);
            return Result.success("增加成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.fail("增加失败");
        }
    }

    /**
     * 修改
     *
     * @return Result
     */
    @RequestMapping("/update")
    public Result update(@RequestBody Specification specification) {
        log.info("spec update params:{}", specification);
        try {
            specificationService.update(specification);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }

    /**
     * 获取实体
     *
     * @return Specification
     */
    @RequestMapping("/findOne")
    public Specification findOne(Long id) {
        return specificationService.findOne(id);
    }

    /**
     * 批量删除
     *
     * @return Result
     */
    @RequestMapping("/delete")
    public Result delete(Long[] ids) {
        try {
            specificationService.delete(ids);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

    @RequestMapping("/selectOptionList")
    public List<Map> selectOptionList() {
        return specificationService.selectOptionList();
    }
}
